/**
 *
 * @author Brandon
 */
package WordHandling;
import java.util.*;
import java.io.*;

import project3.StemInfo;
import project3.StemInfo.StemInstance;

/** Class for building the inverted index. */
public class InvertedIndexer implements project3.DocumentInfo{

	/** 
	 * The hash map contains a key (the token word encountered) and a stemInfo object
	 *  for each key.
	 */
	Map<String,StemInfo> stems = new HashMap<String,StemInfo>(); /* actual inverted index, a hash map H*/
	/** The list of tokenized words, set to whatever is returned by Tokenizer.getFiles */
    List<List<String>> documentList = new ArrayList<List<String>>();
    /** The list of files in the directory. */
    ArrayList<File> fileList = new ArrayList<File>();
    /** A list of basic instance info, like document number and start index (may not be implemented). */
    ArrayList<Object> bones = new ArrayList<Object>();
    /** An arraylist of all words. */
    ArrayList<String> allInstances = new ArrayList<String>();
    /** Stemmer object for turning the tokenized words from documentList into stems. */
    Stemmer s = new Stemmer();
    /** Tokenizer object for splitting the documents into tokens. */
    Tokenizer t= new Tokenizer();
    /** The name of the directory. */
    File directory = new File(toString());
    
	/**
	 * Constructs an inverted index which contains all stem information and
	 * instances in a collection of text documents.
	 * @param fileNums a list of the files in the directory.
	 * @param tokenizedDocs a 2D array of tokenized words, set to whatever 
	 * is returned by Tokenizer.getFiles
	 */
    InvertedIndexer(File dir){
    	
    	directory = dir;
    	//fileList = fileNums;
    	//documentList = tokenizedDocs;
    	
        //File Chooser gets directory containing all files to be processed
        
      /*  String curDir = System.getProperty("user.dir");
        JFileChooser fc = new JFileChooser(curDir);
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int returnVal = fc.showOpenDialog(null);
        if(returnVal == JFileChooser.APPROVE_OPTION) {
         File selection = fc.getSelectedFile();
      	   System.out.println("You chose to open this file: " + selection.getName());
        */
      	 //Tokenize takes the entire directory, breaks it down into lists of strings excluding stop words and punctuation
      	 t.Tokenize(dir);
      	 
        
        
        /** Stem every token word in the <code>documentList</code> and add it to the 
         *  index. Add the instances to an arraylist.*/
        for(int i=0; i<t.getFiles().size(); i++){
        	for(int j=0; j<t.getFiles().get(i).size(); j++){
        		String word = t.getFiles().get(i).get(j);
        		String stem = s.stem(word);
        		//Collection<StemInstance> instances = new ArrayList<StemInstance>();
        		if(stems.containsKey(stem)){
        			stems.get(stem).addInstance(t.getDocumentList().get(i), j, 0, word);
        			/*instances = stems.get(stem).getInstances();
        	    	for(StemInstance instance : instances){
        	    		allInstances.add(instance);
        	    	}*/
        			}else{
        				StemInfo stemInfo = new StemInfo(stem);
        				stemInfo.addInstance(t.getDocumentList().get(i), j, 0, word);
        				stems.put(stem, stemInfo);
            	    	/*for(StemInstance instance : instances){
            	    		allInstances.add(stemInfo.getInstances());
            	    	}*/
            	    	}
        		}
        	}
        }
    
    /** Populate a list containing basic info for each stem (document number and start index);
     * may not be implemented, Mark can use something like this iterator method to 
     * directly access what he needs */
    public ArrayList<Object> basicInfo(String stem){	
    	Collection<StemInstance> instances = stems.get(stem).getInstances();
    	Iterator<StemInstance> iter = instances.iterator();
    	while(iter.hasNext()){
    	  StemInstance stemInstance = iter.next();
    	  	bones.add(stemInstance.getDocument());
    	  	bones.add(stemInstance.getStartIndex());
    	}	
    	return bones;
    }
    
    /** Returns the list of all instances of all stems in all documents. */
    @SuppressWarnings("unchecked")
	public ArrayList<String> allInstances(){
    	Set set = new HashSet();
    	set = stems.keySet();
    	Iterator iter = set.iterator();
    	ArrayList<String> allWords = new ArrayList<String>();
    	while(iter.hasNext()){
    		allWords.add((String) iter.next());
    	} 	
    	return allWords;
    }
    
	@Override
	/** The implementation of <code>documentInfo</code>'s getInstances method.  Returns
	 *  all of the instances of a stem in the given document
	 * @param stem a word stem
     * @return a set of all instances of the provided <code>stem</code> or null
     * if no instances of the stem exist in the document.*/
	public Collection<StemInstance> getInstances(String stem) {
		Collection<StemInstance> si = new ArrayList<StemInstance>();
		Collection<StemInstance> swineflu = new ArrayList<StemInstance>();
		if(stems.get(stem) != null){
		swineflu = stems.get(stem).getInstances();
		}
		for(int i=0; i<swineflu.size(); i++){
			si.add(((ArrayList<StemInstance>) swineflu).get(i));
		}
			
		return si;
		}
	}



